package com.dm.library.adapter;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.ViewGroup;


/**
 * 版    权:  深圳市迪蒙网络科技有限公司
 * 描    述:  <描述>
 * 修 改 人:  zhoulai
 * 修改时间:  2017/3/27
 */

public class BaseViewPagerAdapter extends FragmentStatePagerAdapter {


    private FragmentPageInfo[] mInfoList;
    private Fragment mCurFragment;
    private Context mContext;

    public BaseViewPagerAdapter(Context context, FragmentManager fm, FragmentPageInfo[] infoList) {
        super(fm);
        mInfoList = infoList;
        this.mContext = context;
    }


    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        if (object instanceof Fragment) {
            mCurFragment = (Fragment) object;
        }
    }

    public Fragment getCurFragment() {
        return mCurFragment;
    }

    @Override
    public Fragment getItem(int position) {
        FragmentPageInfo info = mInfoList[position];
        return Fragment.instantiate(mContext, info.getClx().getName(), info.getArgs());
    }

    @Override
    public int getCount() {
        return mInfoList.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mInfoList[position].getTitle();
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }

    /**
     * 退出重新登录后进入还款界面，
     * 解决bug：java.lang.IllegalStateException: Fragment no longer exists for key f0
     * 2017/7/31 10:13
     */
    @Override
    public Parcelable saveState() {
        return null;
    }

}
